﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Services;


namespace GPCSC.Web
{
    /// <summary>
    /// UPLoadHandler 的摘要说明
    /// </summary>
    ///     /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class UPLoadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
             
            try
            {
                
                context.Response.ContentType = "text/plain";
                context.Response.Charset = "utf-8";
                HttpPostedFile file = context.Request.Files["Filedata"];
                string uploadPath =
                    HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";

                if (file != null)
                {
                    if (!Directory.Exists(uploadPath))
                    {
                        Directory.CreateDirectory(uploadPath);
                    }
                    string type = file.FileName;
                    string typrs = file.ContentType;
                    string name = Guid.NewGuid().ToString().Replace("-", "");
                    string filenames = name + type;
                    file.SaveAs(uploadPath + filenames);
                    //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                    string filesid = context.Request.QueryString["newsid"];
                    AfterFileSave(filesid, filenames, file.FileName, typrs);

                    context.Response.Write("1");

                    //上传文件完成之后

                }
                else
                {
                    context.Response.Write("0");
                }
            }
            catch (Exception ex)
            {
                  Hashtable htable = new Hashtable();
                  htable.Add("@ErrorMeaasge", ex.Message);
                  htable.Add("@RowGuid", Guid.NewGuid().ToString());
                  new SqlReadXml().NotDoSQL("ins_ErrorMessage", htable);
                 
            }
        }

        protected void AfterFileSave(string ids,string filenames,string filename,string types)
        {
            Model.newsfiletable filetable = new Model.newsfiletable();
            filetable.NewsfilesId = Guid.NewGuid();
            filetable.NewsId = new Guid(ids);
            filetable.YfileName = filename;
            filetable.xfileName = filenames;
            filetable.fileUrl = "uploadfile/" + filenames;
            filetable.IsDel = 0;
            filetable.filetype = types;
            filetable.TJdate = DateTime.Now;
            new BLL.newsfiletable().Add(filetable);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}